package com.fucius.study.lintcode;

public class MergeSortedArray {
    /**
     * @param A: sorted integer array A
     * @param B: sorted integer array B
     * @return: A new sorted integer array
     */
    public static int[] mergeSortedArray(int[] A, int[] B) {
        if (A == null) {
            return B;
        }
        if (B == null) {
            return A;
        }
        int[] result = new int[A.length+B.length];
        int i=0,j=0,k=0;
        while (i<A.length && j<B.length) {
            if(A[i]>B[j]){
                result[k++] = B[j++];
            }else{
                result[k++] = A[i++];
            }
        }
        while(i<A.length){
            result[k++] = A[i++];
        }
        while(j<B.length){
            result[k++] = B[j++];
        }
       return result;
    }

    public static void main(String[] args) {
        int[] a = {1,2};
        int[] b = {1,2};
        int[] c = mergeSortedArray(a,b);
        for (int i : c) {
            System.out.println(i);
        }
    }
}
